package com.chima.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.chima.dao.RoseDao;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Resource;
import com.chima.domain.Rose;
import com.chima.service.RoseService;

public class RoseServiceImp implements RoseService {
	private RoseDao roseDao;

	public RoseDao getRoseDao() {
		return roseDao;
	}

	public void setRoseDao(RoseDao roseDao) {
		this.roseDao = roseDao;
	}

	public List<Rose> findallRoses() {
		return roseDao.findallRoses();
	}

	public PageResponseBean findrose(PageRequestBean pageRequestBean) {
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults = pageRequestBean.getLimit();
		int total = roseDao.findByTotalCount();
		// 获取分页查询条件
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Rose.class);
//		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<Rose> roses= roseDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(roses);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
		
		
	}

	public boolean saveorupdate(int id, int id2) {
		return roseDao.saveorupdate(id,id2);
	}

	public boolean saveRose(Rose rose) {
		
		return  roseDao.save(rose);
	}

	public void deleteRose(Integer rid) {
		roseDao.deleteRose(rid);
		
	}

	public void delrose_resource(int rId) {
		roseDao.delRose_resource(rId);
		
	}

	public boolean check(int resourceID, int roseID) {
		 boolean check=false;
		   List ids=new ArrayList();
		     if(roseID!=0){
		    	 List<Resource> l=roseDao.resourcesfindbyroseid(roseID);
		    	  if(l.size()>0){
				    	for(int i=0;i<l.size();i++){
				    		ids.add(l.get(i).getId());
				    	} 
					 }
		     }
		    if(ids.contains(resourceID)){
		    	check=true;
		    }
		return check;
	}

	public void edit(Rose rose) {
		roseDao.editRose(rose);
		
	}

}
